﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GameSchool.Models;


namespace GameSchool.Controllers
{
	[Authorize]
    public class HomeController : Controller
    {
        GameRepository repository = new GameRepository();
		public ActionResult Index()
        {
            ViewBag.Message = "You son of a bizzle pizzle rizzle sizzle augue hgrejifeggregregreijegiegeggeendrerizzle accumnsan. Aenizzle ívarergrgreeregregebleeeeeee in est. Fizzle maurizzle go to hizzle, viverra crackalackin, facilisis , ma nizzle izzle, erizzle. Mammasay mammasa mamma oo sa ante ipsizzle primizzle in hizzle sure luctizzle et fo shizzle my nizzle posuere mammasay mammasa mamma oo sa Mofo; Donizzle pizzle. Integer boom shackalack. Break it down pharetra blandit pizzle. Shiz bow wow wow aliquizzle daahng dawg. Sizzle facilisizzle. Fizzle crackalackin nulla, nizzle check it out, hizzle crunk, blandit izzle, ma nizzle.";

            return View();
        }

		//[Authorize(Roles="Admin")]
		public ActionResult About()
		{
			if (User.IsInRole("Admin"))
			{
				return View();
			}
			else
			{
				return RedirectToAction("Index");
			}
		}

		public ActionResult UserTest()
		{
			var userID = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
			ViewData["bla"] = userID;
			return View();
		}


        public ActionResult Games()
        {
            
            var items = from n in repository.Games()
                        orderby n.ID
                        select n;
            return View(items);
        }








        [HttpGet]
        public ActionResult EditGame(int? id)
        {
            //has.value ? villutékk vantar hjá mér
            //IEnumerable<Game> item = from n in repository.Games()
            var item = (from n in repository.Games()
                        where n.ID==id.Value
                        select n).SingleOrDefault();

            if (item != null)
            {
                return View(item);
            }
            else
            {
                return RedirectToAction("Index"); ;
            }
        }

        [HttpPost]
        public ActionResult EditGame(int? id, FormCollection formData)
        {
            //has.value ? villutékk vantar hjá mér
            //IEnumerable<Game> item = from n in repository.Games()
            var item = (from n in repository.Games()
                        where n.ID == id.Value
                        select n).SingleOrDefault();

            if (item != null)
            {
                //Geri breytingar
                UpdateModel(item);
                

                //Vista breytingar
                repository.Save();
                return RedirectToAction("Games");
            }
            else
            {
                return View();
            }
        }

        
        
        





        /*
         * Hér fyrir neðan eru 2 controllerar.
         * Þeir sjá um að birta formið "GameForm" sem á að vera hægt að nota bæði fyrir EditGame og CreateGame skipunina
         */

        [HttpGet]
        public ActionResult CreateGame()
        {
            return View(new Game());
        }


        [HttpPost]
        public ActionResult CreateGame(FormCollection formData)
        {
            Game newGame = new Game();
            repository.Add(newGame);
            UpdateModel(newGame);
            //setja inn validation
            repository.Save();
            return RedirectToAction("Games");
        }
    }
}
